"""
Adam Prejean
Date Created: 8/21/2014
ap_MixamoTools.py

How to run:
import ap_MixamoTools
reload (ap_MixamoTools)
ap_MixamoTools.ActivateUI()

"""

import maya.cmds as cmds

def ActivateUI():
	# Window Creation =================================================================================================

	if cmds.window( "MixamoWindow", exists=True, q=True ):
		cmds.deleteUI( "MixamoWindow", window=True )
	#if (cmds.windowPref("MixamoWindow", ex=True)):
	#    cmds.windowPref("MixamoWindow", r=True)
		
	window = cmds.window( "MixamoWindow", title = "Mixamo Tools", sizeable=False, mnb=False, mxb=False, toolbox=False)
	form = cmds.formLayout(numberOfDivisions=100)

	# Button Creation =================================================================================================

	# selAll
	object = cmds.button( command=selAll, backgroundColor=(0.25,0.25,0.25), label="selAll", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 6), ( object, 'left', 6)] )
	#=========================================
	
	# selVis
	object = cmds.button( command=selVis, backgroundColor=(0.35,0.35,0.35), enableBackground=False, label="selVis", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 46), ( object, 'left', 6)] )
	#=========================================

	# ZeroOut
	object = cmds.button( command=ZeroOut, backgroundColor=(0.25,0.25,0.25), label="ZeroOut", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 6), ( object, 'left', 78)] )
	#=========================================	
	
	# SetKey
	object = cmds.button( command=SetKey, backgroundColor=(0.35,0.35,0.35), enableBackground=False, label="SetKey", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 46), ( object, 'left', 78)] )
	#=========================================

	# FK/IK Arms
	object = cmds.button( backgroundColor=(0.5,0.15,0.1), enableBackground=False, label="FK/IK Arms", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 6), ( object, 'left', 149)] )
	#=========================================

	# FK/IK Legs
	object = cmds.button( backgroundColor=(0.1,0.15,0.5), enableBackground=False, label="FK/IK Legs", w=66, h=33)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 46), ( object, 'left', 149)] )
	#=========================================



	cmds.setParent( '..' )

	cmds.window( "MixamoWindow", edit=True, widthHeight=(221.0, 85.0)) #For a 3rd Row: change 88.0 to 128.0

	cmds.showWindow( window )


def selAll(self): # select all visible control objects in the current scene
	cmds.select(clear = True)
	try:
		# selects all control objects in the original scene
		cmds.select( "Controls_SelectSet", add = True )

	except ValueError:
		# selects all control objects in the referenced scene
		cmds.select("*:Controls_SelectSet", add = True )


def selVis(self): # select all visible control objects in the current scene
	cmds.select(clear = True)
	try:
		# selects all control objects in the original scene
		cmds.select( "Controls_SelectSet", add = True, visible = True )

	except ValueError:
		# selects all control objects in the referenced scene
		cmds.select("*:Controls_SelectSet", add = True, visible = True )


def SetKey(self): # set keyframe on all "keyable" attributes of the selected objects
	cmds.setKeyframe()


def ZeroOut(self): # reset all transforms on selected control objects
	attrVsDefaultValue = {'sx':1, 'sy':1, 'sz':1, 'rx':0, 'ry':0, 'rz':0, 'tx':0, 'ty':0, 'tz':0}

	sel = cmds.ls(sl=1)
	for obj in sel:
		for attr in attrVsDefaultValue:
			try:
				cmds.setAttr('%s.%s'%(obj, attr), attrVsDefaultValue[attr])
			except:
				pass






